package com.zkingsoft.wechat.pay;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.tools.LogUtil;

public class PayThreadPool {

	private static Map<String, Object> PAY_WAIT_POOL = new HashMap<>();
	private static Map<String, AjaxResult> PAY_THREAD_MSG = new HashMap<>();

	public static void waitThread(String orderId, Object lock) {
		PAY_WAIT_POOL.put(orderId, lock);
		synchronized (lock) {
			try {
				// 设置线程最多等待15s
				lock.wait(1000 * 15);
			} catch (InterruptedException e) {
				LogUtil.debug("线程等待失败", e);
			}
		}

	}

	public static AjaxResult getThreadResult(String orderId) {
		return PAY_THREAD_MSG.get(orderId);
	}

	public static void notifyThread(String orderId, AjaxResult result) {
		Object lock = PAY_WAIT_POOL.get(orderId);
		if (lock != null) {
			synchronized (lock) {
				LogUtil.debug("尝试释放锁{}", orderId);
				PAY_THREAD_MSG.put(orderId, result);
				PAY_WAIT_POOL.remove(orderId);
				lock.notifyAll();
			}
		}
	}

}
